package com.zou.service.impl;

import com.zou.commons.domain.ReturnObject;
import com.zou.mapper.MaxMenuMapper;
import com.zou.pojo.MaxMenu;
import com.zou.pojo.MinMenu;
import com.zou.service.MaxMenuService;
import com.zou.service.MinMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("maxMenuService")
@Transactional
public class MaxMenuServiceImpl implements MaxMenuService {
    @Autowired
    private MaxMenuMapper maxMenuMapper;
    @Autowired
    private MinMenuService minMenuService;

    @Override
    public List<MaxMenu> querySomeMaxMenuByMaxMenu(MaxMenu maxMenu) {
        return maxMenuMapper.selectSomeMaxMenuByMaxMenu(maxMenu);
    }

    @Override
    public boolean queryIsExistMaxName(String maxName) {
        // 为空说明不存在
        MaxMenu maxMenu = maxMenuMapper.selectIsExistMaxName(maxName);
        if (maxMenu == null) {
            return false;
        }
        return true;
    }

    @Override
    public ReturnObject editMaxMenu(Integer id, String name) {
        ReturnObject returnObject = new ReturnObject();
        if (queryIsExistMaxName(name)) {
            returnObject.setFail("此名字已存在");
        } else {
            try {
                if (maxMenuMapper.updateMaxMenu(new MaxMenu().setMaxId(id).setMaxName(name)) > 0) {
                    returnObject.setSuccess();
                } else {
                    returnObject.setFail("系统忙，请稍后重试");
                }
            } catch (Exception e) {
                e.printStackTrace();
                returnObject.setFail("系统忙，请稍后重试");
            }
        }
        return returnObject;
    }

    @Override
    public ReturnObject addMaxMenu(String name) {
        ReturnObject returnObject = new ReturnObject();
        if (queryIsExistMaxName(name)) {
            returnObject.setFail("此名字已存在");
        } else {
            try {
                if (maxMenuMapper.insertMaxMenu(name) > 0) {
                    returnObject.setSuccess();
                } else {
                    returnObject.setFail("系统忙，请稍后重试");
                }
            } catch (Exception e) {
                e.printStackTrace();
                returnObject.setFail("系统忙，请稍后重试");
            }
        }
        return returnObject;
    }

    @Override
    public ReturnObject delMaxMenuById(Integer id) {
        ReturnObject returnObject = new ReturnObject();
        List<MinMenu> minMenus = minMenuService.querySomeMinMenuByMinMenu(new MinMenu().setMaxId(id));
        if (minMenus.size() != 0) {
            returnObject.setFail("请先删除本菜单下的下拉菜单");
        } else {
            try {
                if (maxMenuMapper.deleteMaxMenu(id) > 0) {
                    returnObject.setSuccess();
                } else {
                    returnObject.setFail("系统忙，请稍后重试");
                }
            } catch (Exception e) {
                e.printStackTrace();
                returnObject.setFail("系统忙，请稍后重试");
            }
        }
        return returnObject;
    }
}
